import { defineConfig, loadEnv } from 'vite';
import type { UserConfig } from 'vite';
import { createVitePlugins } from './config/plugin';
import { resolve } from 'path';
import { configManualChunk } from './config/optimizer';

function pathResolve(dir: string) {
  return resolve(process.cwd(), '.', dir);
}

// https://vitejs.dev/config/
export default ({ mode }) => {
  let base = '/';
  let isBuild = false;
  switch (process.env.npm_lifecycle_event) {
    case 'start:develop':
      isBuild = false;
      break;
    case 'build:develop':
      isBuild = true;
      break;
    case 'build:testing':
      isBuild = true;
      break;
    case 'build:product':
      isBuild = true;
      break;
  }
  console.log('mode=>', mode);
  return defineConfig({
    base: "./",
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'), //把src改为@
      },
    },
    plugins: createVitePlugins(isBuild),
    server: {
      host: '0.0.0.0',
      port: 3000,
      open: true,
      hmr: true,
    },
    css: {
      preprocessorOptions: {
        scss: {
          charset: false,
        },
        less: {
          charset: false,
          modifyVars: {
            'primary-color': '#7546c9',
            'link-color': '#7546c9',
            'border-color-base': '#DDDDDD',
            'border-radius-base': '15px',
            'height-base': '30px',
            'table-header-bg': '#F5F5F5',
            'table-border-color': '#DDDDDD',
            'table-padding-vertical':'4px',
            'table-border-radius-base': '0px'
          },
          javascriptEnabled: true,
        },
      },
    },
    build: {
      sourcemap: false,
      minify: false,
      chunkSizeWarningLimit: 1500,
      // terserOptions: {
      //   compress: {
      //     drop_console: false,
      //     drop_debugger: false,
      //   },
      // },
      rollupOptions: {
        output: {
          manualChunks: configManualChunk,
        },
      },
      //Turning off reportCompressedSize display can slightly reduce packaging time
      reportCompressedSize: false,
    },
  });
};
